Skip to content

feat: add per-resource label selector support#2922

Open
superbrothers wants to merge 1 commit into
kubernetes:mainfrom
superbrothers:label-selector
Open

feat: add per-resource label selector support#2922
superbrothers wants to merge 1 commit into
kubernetes:mainfrom
superbrothers:label-selector

Conversation

@superbrothers
Copy link
Copy Markdown
Member

What this PR does / why we need it:

This PR adds per-resource label selector support for built-in resources in kube-state-metrics.

Users can now repeat --label-selector to scope individual resources by Kubernetes label selector, for example:

--label-selector=nodes=tenant=team-a
--label-selector='pods=app in (frontend,api)'

This allows KSM to reduce the watched and exposed object set for selected resources, which is useful for tenant isolation, sharding, and reducing API server / memory usage.

Compared to the original proposal in #2885, this PR intentionally changes the CLI shape:

  1. It uses a repeatable --label-selector flag instead of a compact --label-selectors=nodes=...,pods=... form.
  2. This avoids introducing a custom escaping / quoting grammar, since valid Kubernetes label selectors can already contain commas and spaces.

This PR supports built-in resources only. Custom resource metrics are not changed by this PR.

How does this change affect the cardinality of KSM: (increases, decreases or does not change cardinality)

Does not increase cardinality. For filtered resources, it can reduce the number of exposed series by reducing the set of watched objects.

Which issue(s) this PR fixes: (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged)

Closes #2885

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Apr 12, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

This issue is currently awaiting triage.

If kube-state-metrics contributors determine this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: superbrothers
Once this PR has been reviewed and has the lgtm label, please assign dgrisonnet for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@github-project-automation github-project-automation Bot moved this to Needs Triage in SIG Instrumentation Apr 12, 2026
@k8s-ci-robot k8s-ci-robot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Apr 12, 2026
Signed-off-by: Kazuki Suda <kazuki.suda@gmail.com>
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

PR needs rebase.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

Add per-resource label selector support via --label-selectors flag

2 participants